package com.recordhistory.service;

import com.recordhistory.Constant;
import com.recordhistory.dao.HistoryMainDao;
import com.recordhistory.exception.ServiceException;
import com.recordhistory.model.EventOccur;
import com.recordhistory.model.HistoryMain;
import com.recordhistory.model.Period;
import com.recordhistory.model.ReignTitle;
import com.recordhistory.utils.CheckTool;
import com.recordhistory.utils.CommonTool;
import com.recordhistory.vo.EventOccurVo;
import com.recordhistory.vo.HistoryMainVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：LX
 * 创建时间： 2019/6/12. 17:20
 * 地点：广州
 * 目的: 历史主表
 * 备注说明：
 */
@Service
public class HistoryMainService {

    @Resource
    private HistoryMainDao historyMainDao;
    @Autowired
    private EventOccurService eventOccurService;
    @Autowired
    private PeriodService periodService;
    @Autowired
    private ReignTitleService reignTitleService;




    /**
     * 根据 公元纪年 查询 主干，如果传入ID则不包含这个id的
     * @param ad 公元纪年
     * @param id 主干主键
     * @return
     */
    public HistoryMain selectHistoryMainByAdNotThis(Integer ad, String id){
        return historyMainDao.selectHistoryMainByAdNotThis(ad, id);
    }


    /**
     * 根据 主键查询
     * @param id 历史主线ID
     * @return
     */
    public HistoryMain selectHistoryMainById(String id){
        if (CheckTool.checkNull(id)){
            throw new ServiceException("主键ID不能为空");
        }
        return historyMainDao.selectById(id);
    }


    /**
     * 查询全部的 公元纪年 并根据公元纪年排序
     * @return
     */
    public List<HistoryMain> selectAllAdSort(){
        return historyMainDao.selectAllAdSort();
    }

    /**
     * 查询全部的 公元纪年 并根据公元纪年排序
     * @return
     */
    public List<HistoryMainVo> selectVoAllAdSort(){
        return historyMainDao.selectVoAllAdSort();
    }

    /**
     * 历史主干 检查主要项目
     * @param historyMain
     */
    private void checkHistoryMain(HistoryMain historyMain){
        if (historyMain == null){
            throw new ServiceException("不能添加空数据");
        }
        if (historyMain.getAd() == null){
            throw new ServiceException("公元纪年是必填项目");
        }
        if (historyMain.getAd() == 0){
            throw new ServiceException("不存在公元0年，公元元年从公元一年开始");
        }
        HistoryMain historyMainSQL = historyMainDao.selectHistoryMainByAdNotThis(historyMain.getAd(), historyMain.getId());
        if (historyMainSQL != null){
            throw new ServiceException("已经存在一个同样的公元纪年，不能重复添加");
        }
    }


    /**
     * 新增 历史主干
     * @param historyMain 历史主干data
     */
    @Transactional(rollbackFor = Exception.class)
    public void addHistoryMain(HistoryMain historyMain){
        checkHistoryMain(historyMain);

        if (CheckTool.checkNull(historyMain.getAccuracy())){
            historyMain.setAccuracy(Constant.ACCURACY);
        }

        historyMain.setId(CommonTool.getUUId());
        historyMain.setCreateDate(CommonTool.getTimestamp());
        historyMain.setUpdateDate(CommonTool.getTimestamp());

        historyMainDao.insert(historyMain);
    }


    /**
     * 更新 历史主干
     * @param historyMain 历史主干data
     */
    @Transactional(rollbackFor = Exception.class)
    public void updHistoryMain(HistoryMain historyMain){
        checkHistoryMain(historyMain);

        if (CheckTool.checkNull(historyMain.getId())){
            throw new ServiceException("更新异常，参数丢失，请重试");
        }
        historyMain.setUpdateDate(CommonTool.getTimestamp());

        historyMainDao.updateById(historyMain);
    }


    /**
     * 根据主键删除 历史主干
     * @param id 历史主干data
     */
    @Transactional(rollbackFor = Exception.class)
    public void delHistoryMain(String id){
        if (CheckTool.checkNull(id)){
            throw new ServiceException("ID不能为空");
        }
        //删除各种关联信息
        List<EventOccurVo> eventOccurList = eventOccurService.selectEventBySeason(id);
        if (eventOccurList != null && eventOccurList.size() > 0){
            for (EventOccur eventOccur : eventOccurList){
                eventOccurService.delEventOccur(eventOccur.getId());
            }
        }
        //删除历史时期
        List<Period> periods = periodService.selectPeriodByHistoryIdStart(id);
        if (periods != null && periods.size() > 0){
            for (Period period : periods){
                periodService.delPeriod(period.getId());
            }
        }
        //删除年号相关
        List<ReignTitle> reignTitles = reignTitleService.selecByHtistoryId(id);
        if (reignTitles != null && reignTitles.size() > 0){
            for (ReignTitle reignTitle : reignTitles){
                reignTitleService.delReignTitle(reignTitle.getId());
            }
        }

        historyMainDao.deleteById(id);
    }

}
